from django.utils.deprecation import MiddlewareMixin
from stw_app import models

class AuthMiddleware(MiddlewareMixin):
    def process_request(self,request):
        """如果用户已经登录，则request中赋值"""
        # 获取session中的userid，即当前登录的id号
        # user_id = request.session.get('userid')

        # # 测试用，
        # request.session['userid'] = 10017
        try:
            user_id = models.Code.objects.filter(id1=3).first().code_1
        except:
            user_id = None

        # 获取当前id对应的object对象，有或者没有
        user_obj = models.UserInfo.objects.filter(id=user_id).first()
        # 之后可以通过request.user_obj来获取当前用户对象，是user_info表中的对象
        request.user_obj = user_obj

